﻿<?php
class weibo extends spController{
	private $AKEY = '1424845523';//填入APPKEY
	private $SKEY = 'd94a492850103eff037ea18b206aa0dc';//填入APPSECRET
	
	function index(){
		
	}
	
	function call(){

		$o = spClass('WeiboOAuth',array($this->AKEY,$this->SKEY));
		$URI = 'http://'.$_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].$_SERVER['SCRIPT_NAME'];
		$keys = $o -> getRequestToken();
		$aurl = $o -> getAuthorizeURL( $keys['oauth_token'] ,false , $URI.'?c=weibo&a=back');
		$_SESSION['keys'] = $keys;
		echo 'Jump to Weibo,Wait.';
		$this -> jump($aurl,2);
	}
	
	function back(){
		$o = spClass('WeiboOAuth',array($this->AKEY,$this->SKEY,$_SESSION['keys']['oauth_token'],$_SESSION['keys']['oauth_token_secret']));
		$last_key = $o -> getAccessToken($this -> spArgs('oauth_verifier'));
		$c = new WeiboClient($this->AKEY,$this->SKEY,$last_key['oauth_token'],$last_key['oauth_token_secret']);
		$msg = $c -> verify_credentials();
		if ($msg === false || $msg === null){
			echo "Error occured";
			return false;
		}
		if (isset($msg['error_code']) && isset($msg['error'])){
			echo ('Error_code: '.$msg['error_code'].';  Error: '.$msg['error'] );
			return false;
		}
		if (isset($msg['name'])){
			$result = spClass('libICase') -> acl_connect_login('WEIBO',$msg['id'],$msg['name'],spClass('spICase')->get(),'');
			if($result > 0){
				spClass('spICase') -> set($result);
				$this->success('登录成功',spUrl());//这里填写跳转地址
			}
			elseif($result == '-11'){
				$_SESSION['tmp_webuser'] = $msg['id'];
				$_SESSION['tmp_webusername'] = $msg['name'];
				$this -> display('main_register.html');
			}
			else{
				echo '错误码'.$result;
				die();
			}
		}
	}
	
	function emailregister(){
		$result = spClass('libICase') -> acl_connect_login('WEIBO',$_SESSION['tmp_webuser'],$_SESSION['tmp_webusername'],NULL,$this->spArgs('email'));
		if($result > 0){
			spClass('spICase') -> set($result);
			$this->success('登录成功',spUrl());//这里填写跳转地址
		}
		elseif($result == '-11'){
			$this -> display('main_register.html');
		}
		else{
			echo '错误码'.$result;
			die();
		}
	}
	
	function loginregister(){
		$login = spClass('libICase') -> acl_user_login($this -> spArgs('uname'),$this -> spArgs('upass'));
		if($login['id'] > 0){
			$result = spClass('libICase') -> acl_connect_login('WEIBO',$_SESSION['tmp_webuser'],$_SESSION['tmp_webusername'],$login['id']);
			spClass('spICase') -> set($result);
			$this->success('登录成功',spUrl());//这里填写跳转地址
		}
		else{
			$this -> display('main_register.html');
		}
	}
}